package com.guli.gulimall.product.service.impl;

import com.guli.gulimall.product.dao.*;
import com.guli.gulimall.product.entity.SkuImagesEntity;
import com.guli.gulimall.product.entity.SpuInfoDescEntity;
import com.guli.gulimall.product.vo.ItemSaleAttrVo;
import com.guli.gulimall.product.vo.SkuItemVo;
import com.guli.gulimall.product.vo.SpuItemAttrGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.entity.SkuInfoEntity;
import com.guli.gulimall.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    SkuInfoDao skuInfoDao;

    @Autowired
    SkuImagesDao skuImagesDao;

    @Autowired
    SkuSaleAttrValueDao skuSaleAttrValueDao;

    @Autowired
    SpuInfoDescDao spuInfoDescDao;

    @Autowired
    ProductAttrValueDao productAttrValueDao;

    @Autowired
    ThreadPoolExecutor executor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public SkuItemVo getItemBySkuId(Integer skuId) throws ExecutionException, InterruptedException {

        SkuItemVo skuItemVo = new SkuItemVo();

        //1 sku基本信息
        CompletableFuture<Long> future = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfoEntity = skuInfoDao.selectById(skuId);
            skuItemVo.setSkuInfoEntity(skuInfoEntity);
            return skuInfoEntity.getSpuId();
        },executor);


        //2 sku所有图片
        CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> imags = skuImagesDao.selectList(new QueryWrapper<SkuImagesEntity>().eq("sku_id", skuId));
            skuItemVo.setImags(imags);
        });


        //3.todo 是否有库存

        //4.获取spu的销售属性组合。每个attrName对应一个value-list
        CompletableFuture<Void> future3 = future.thenAcceptAsync(spuid -> {
            List<ItemSaleAttrVo> saleAttr = skuSaleAttrValueDao.getItemSaleAttrVo(spuid);
            skuItemVo.setSaleAttr(saleAttr);
        });


        //spu介绍
        CompletableFuture<Void> future4 = future.thenAcceptAsync(spuid -> {
            SpuInfoDescEntity spuInfoDescEntity = spuInfoDescDao.selectById(spuid);
            skuItemVo.setSpuInfoDescEntity(spuInfoDescEntity);
        });

        //规格属性
        CompletableFuture<Void> future5 = future.thenAcceptAsync(spuid -> {
            List<SpuItemAttrGroup> groupAttrs = productAttrValueDao.getSpuItemAttrGroup(spuid);
            skuItemVo.setGroupAttrs(groupAttrs);
        });


        CompletableFuture.allOf(future,future2,future3,future4,future5).get();
        return skuItemVo;
    }

}